import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import '../../val_application_base_root_page.dart';
import '../../pages/login/val_reminder_login_page.dart';
import 'val_app_state_tool.dart';

class ValRouterManager {
  ///获取入口
  static Widget getRoot() {
    return ValAppStateTool().token.isNotEmpty ? const ValApplicationBaseRootPage() : const ValReminderLoginPage();
  }

  /// 去登录页面
  static void gotoLogin() async{
    await Get.offAll(() => const  ValReminderLoginPage());
  }

  /// 去主页
  static void gotoMain() async{
    await Get.offAll(() => const  ValApplicationBaseRootPage());
  }

  ///关闭路由
  static close() {
    Get.back();
  }

  ///关闭路由 返回数据
  static closeWithResult<T>(T result) {
    Get.back(result: result);
  }

  static void open<T>(
      T page, {
        bool preventDuplicates = false,
        VoidCallback? back,
        Transition? transition,
        Duration? duration,
        bool fullscreenDialog = false,
        bool opaque = true,
        int? id
      }) {
    Get.to(() => page,
        preventDuplicates: preventDuplicates,
        transition: transition,
        duration: duration,
        id: id,
        fullscreenDialog: fullscreenDialog,
        opaque: opaque)
        ?.whenComplete(() => back?.call());
  }

  ///打开页面 有返回结果
  static Future<T> openForResult<T>(
      dynamic page, {
        bool preventDuplicates = false,
        Transition? transition,
        Duration? duration,
        bool fullscreenDialog = false,
        bool opaque = true,
      }) async {
    return await Get.to(page,
        preventDuplicates: preventDuplicates,
        transition: transition,
        duration: duration,
        fullscreenDialog: fullscreenDialog,
        opaque: opaque);
  }

  static void off<T>(
      T page, {
        bool preventDuplicates = false,
        VoidCallback? back,
        Transition? transition,
        Duration? duration,
        bool fullscreenDialog = false,
      }) {
    Get.off(
          () => page,
      preventDuplicates: preventDuplicates,
      transition: transition,
      duration: duration,
      fullscreenDialog: fullscreenDialog,
    )?.whenComplete(() => back?.call());
  }

  static void until(
      String pageName, {
        bool preventDuplicates = false,
        VoidCallback? back,
        Transition? transition,
        Duration? duration,
        bool fullscreenDialog = false,
      }) {
    Get.until((route) => route.settings.name == pageName);
  }
}